package dhjang.gui;

import java.awt.GridBagLayout;
import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.BoxLayout;
import javax.swing.JTextField;
import java.awt.Rectangle;
import javax.swing.JSlider;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.BorderLayout;
import java.awt.FlowLayout;

public class GUI_TextSlider_Float extends JPanel {

	private static final long serialVersionUID = 1L;
	private JTextField jTextField = null;
	private JSlider jSlider = null;
	
	private float m_value;
	private float m_resolution;

	/**
	 * This is the default constructor
	 */
	public GUI_TextSlider_Float() {
		super();
		initialize();
		
		setResolution(1000);
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(280, 50);
		this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		this.add(getJTextField(), null);
		this.add(getJSlider(), null);
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					setFloatValue(Float.valueOf(jTextField.getText()));
				}
			});
		}
		return jTextField;
	}

	/**
	 * This method initializes jSlider	
	 * 	
	 * @return javax.swing.JSlider	
	 */
	private JSlider getJSlider() {
		if (jSlider == null) {
			jSlider = new JSlider();
			jSlider.addChangeListener(new javax.swing.event.ChangeListener() {
				public void stateChanged(javax.swing.event.ChangeEvent e) {
					System.out.println("stateChanged(value)"); // TODO Auto-generated property Event stub "value"
					setFloatValue(jSlider.getValue()/getResolution());
				}
			});
			jSlider.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
				public void propertyChange(java.beans.PropertyChangeEvent e) {
					if ((e.getPropertyName().equals("value"))) {
						System.out.println("propertyChange(value)"); // TODO Auto-generated property Event stub "value" 
					}
				}
			});
		}
		return jSlider;
	}

	public void setFloatValue(float m_value) {
		this.m_value = m_value;
		jTextField.setText(Float.toString(m_value));
		
		int val = (int) (m_value * getResolution());
		if(jSlider.getMinimum() > val)
			jSlider.setMinimum((int)val);
		if(jSlider.getMaximum() < val)
			jSlider.setMaximum((int)val);

		jSlider.setValue( val);
	}

	public float getFloatValue() {
		return m_value;
	}

	
	void updateTextFieldValue(JTextField field, float val)
	{
		field.setText(Float.toString(val));
	}

	public void setResolution(float resolution) {
		m_resolution = resolution;
	}

	public float getResolution() {
		return m_resolution;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
